<?php


namespace common\exception;


use Exception;
use think\AccessCors;
use think\exception\Handle;

/**
 * 异常拦截
 * Class ExceptionHandle
 * @package common\Handle
 */
class ExceptionHandle extends Handle
{
    public function render(Exception $e)
    {
        $errorData = [
            "code" => $e->getCode(),
            "msg" => $e->getMessage(),
            "data" => [],
        ];
        // 重写 code为零的异常码为 -500
        $errorData["code"] = $errorData["code"] == 0 ? -500 : $errorData["code"];

        if (APP_DEBUG) {
            $errorData["file"] = $e->getFile();
            $errorData["line"] = $e->getLine();
            $errorData["trace"] = $e->getTrace();
            $errorData["previous"] = $e->getPrevious();
        }

        return json($errorData, 200, AccessCors::check(\request()));
    }

}